/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { hvigorCore } from '../../../external/core/hvigor-core.js';
import { HvigorCoreNode } from '../../../external/core/hvigor-core-node.js';

import { getModuleNameFromTaskPath } from './task-path-util.js';

/**
 * 通过Task的path分割获取其所属Module
 *
 * @param {string} taskPath
 * @returns {HvigorNode | undefined}
 */
export function getModuleFromTaskPath(taskPath: string): HvigorCoreNode | undefined {
  const moduleName = getModuleNameFromTaskPath(taskPath);
  const project = hvigorCore.getProject();
  let module: HvigorCoreNode | undefined;
  if (moduleName === '') {
    module = project;
  } else {
    module = project!.findModuleByName(moduleName);
  }
  return module;
}
